package cn.xmoit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class ClientLoginController {

	@Autowired
	private OAuth2ClientProperties oauth2ClientProperties;

	@Value("${security.oauth2.client.access-token-uri}")
	private String accessTokenUri;

	@PostMapping("/client-login")
	public OAuth2AccessToken login() {
		// 创建 ClientCredentialsResourceDetails 对象
		ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
		resourceDetails.setAccessTokenUri(accessTokenUri);
		resourceDetails.setClientId(oauth2ClientProperties.getClientId());
		resourceDetails.setClientSecret(oauth2ClientProperties.getClientSecret());
		// 创建 OAuth2RestTemplate 对象
		OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails);
		restTemplate.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider());
		// 获取访问令牌
		return restTemplate.getAccessToken();
	}

}
